//
//  LTLEmoticonManager.m
//  WeiBo
//
//  Created by LiTaiLiang on 16/11/14.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLEmoticonManager.h"
#import "LTLEmotion.h"


@interface LTLEmoticonManager ()

@property(nonatomic,strong)NSMutableArray *recentEmoticonList;

@end
/// 每页显示的表情数量
static NSInteger LTLEmoticonsCountOfPage = 20;
/// 表情工具类 - 提供表情视图的所有数据及处理逻辑
@implementation LTLEmoticonManager


#pragma mark - 单例 & 构造函数
+ (instancetype)sharedManager {
    static id instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}
//取数据
- (instancetype)init {
    self = [super init];
    if (self) {
        self.packages = [NSMutableArray array];
        
        NSMutableArray *Lately = [NSMutableArray array];
        [self.packages addObject:Lately];

        [self AddElement:@"default"];
        [self AddElement:@"emoji"];
        [self AddElement:@"lxh"];
        
    }
    return self;
}
-(void)AddElement:(NSString *)Catalog
{
    //拼接路径
    NSString *Default=[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Emoticon/%@/info.plist" ,Catalog] ofType:nil];
    
    NSArray  *array =[NSArray arrayWithContentsOfFile:Default];
    
    NSMutableArray *temporary = [NSMutableArray array];
    //转模型
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        LTLEmotion *model = [[LTLEmotion alloc]initDict:obj];
        model.Catalog = Catalog;
        [temporary addObject:model];
    }];
    
    [self.packages addObject:temporary];


}


#pragma mark - 数据源方法
- (NSInteger)numberOfPagesInSection:(NSInteger)section {
    NSArray *package = _packages[section];
    
    //因为是20个表情+删除按钮 所以 LTLEmoticonsCountOfPage + 1
    //求行从0算 -1
    return ((NSInteger)package.count - 1) / LTLEmoticonsCountOfPage + 1;
}

- (NSArray *)emoticonsWithIndexPath:(NSIndexPath *)indexPath {
    ///取出表情组
    NSArray *package = self.packages[indexPath.section];
    ///开始长度
    NSInteger location = indexPath.item * LTLEmoticonsCountOfPage;
    ///表情长度
    NSInteger length = LTLEmoticonsCountOfPage;
    // 判断取得时候长度是否越界
    if ((location + length) > package.count) {
        length = package.count - location;
    }
    ///取数组元素
    NSRange range = NSMakeRange(location, length);
    //返回数组元素
    return [package subarrayWithRange:range];
}
#pragma mark - 最近使用表情
- (void)addRecentEmoticon:(LTLEmotion *)emoticon {
    // 0. 表情计数 ++
    emoticon.times++;
    
    // 1. 判断表情是否已经存在
    if (![_recentEmoticonList containsObject:emoticon]) {
        [_recentEmoticonList addObject:emoticon];
    }
    
    // 2. 排序
    [_recentEmoticonList sortUsingComparator:^NSComparisonResult(LTLEmotion *obj1, LTLEmotion *obj2) {
        return obj1.times < obj2.times;
    }];
    
    // 3. 更新最近表情包
    [self updateRecentPackage];
    
    // 4. 保存表情包
   
}

/// 更新最近表情包数组
- (void)updateRecentPackage {
    NSInteger length = _recentEmoticonList.count < LTLEmoticonsCountOfPage ? _recentEmoticonList.count : LTLEmoticonsCountOfPage;
    
    _packages[0] = [_recentEmoticonList subarrayWithRange:NSMakeRange(0, length)].mutableCopy;
}

/// 保存最近表情数组

/// 加载最近表情数组


/// 最近表情文件路径
- (NSString *)filePathForRecentEmoticon {
    
    NSString *dir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    
    return [dir stringByAppendingPathComponent:@"baiqing.plist"] ;
}


@end
